home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 395_01 / avl / stckallc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-04  |  2.8 KB  |  121 lines

  1. /* implementation file for the stack allocation package. */
  2.  
  3. /* get definition of size_t */
  4. #include <stdlib.h>
  5.  
  6. #include "align.h"
  7. #include "stckallc.h"
  8.  
  9. /* target size for each allocation block */
  10. #define TARGET_ALLOC 128
  11.  
  12. #define N_UNITS_IN_POINTER N_ALIGN_UNITS(sizeof(void *))
  13.  
  14. /* initialize allocation stack */
  15. void init_alloc_stack
  16.   (
  17.     /* pointer to allocation stack to initialize */
  18.     ALLOC_STACK *as,
  19.     /* sizeof elements to be allocated */
  20.     size_t elem_size
  21.   )
  22.   {
  23.     as->n_units_in_element = N_ALIGN_UNITS(elem_size);
  24.  
  25.     as->n_units_in_block =
  26.       (N_ALIGN_UNITS(TARGET_ALLOC) - N_UNITS_IN_POINTER +
  27.          as->n_units_in_element - 1) / as->n_units_in_element;
  28.     as->n_units_in_block *= as->n_units_in_element;
  29.     as->n_units_in_block += N_UNITS_IN_POINTER;
  30.  
  31.     /* set free index so that a new block will be allocated on the
  32.        1st call to get_alloc_stack() */
  33.     as->free = as->n_units_in_block;
  34.  
  35.     as->block_list = (void *) 0;
  36.  
  37.     return;
  38.   }
  39.  
  40. /* allocate an element from the allocation stack.  returns null if
  41.    insufficient memory available */
  42. void *get_alloc_stack
  43.   (
  44.     /* pointer to allocation stack */
  45.     ALLOC_STACK *as
  46.   ) 
  47.   {
  48.     void *result;
  49.  
  50.     if (as->free == as->n_units_in_block)
  51.       {
  52.         /* get another block from the heap */
  53.     result = malloc(as->n_units_in_block * sizeof(ALIGN_TYPE));
  54.     if (!result)
  55.       return((void *) 0);
  56.     /* insert the block in the list of blocks at the beginning */
  57.     *((void **) result) = as->block_list;
  58.     as->block_list = result;
  59.  
  60.     as->free = N_UNITS_IN_POINTER;
  61.       }
  62.  
  63.     result = ((ALIGN_TYPE *) as->block_list) + as->free;
  64.     as->free += as->n_units_in_element;
  65.  
  66.     return(result);
  67.   }
  68.  
  69. /* return address of last element allocated that was not later freed */
  70. void *look_alloc_stack
  71.   (
  72.     /* pointer to allocation stack */
  73.     const ALLOC_STACK *as
  74.   ) 
  75.   {
  76.     return(((ALIGN_TYPE *) as->block_list) +
  77.            (as->free - as->n_units_in_element));
  78.   }
  79.  
  80. /* free last element allocated */
  81. void free_alloc_stack
  82.   (
  83.     /* pointer to allocation stack */
  84.     ALLOC_STACK *as
  85.   )
  86.   {
  87.     as->free -= as->n_units_in_element;
  88.     if (as->free == N_UNITS_IN_POINTER)
  89.       {
  90.     void *p = as->block_list;
  91.     as->block_list = *((void **) p);
  92.     free(p);
  93.     as->free = as->n_units_in_block;
  94.       }
  95.  
  96.     return;
  97.   }
  98.  
  99. /* free all elements allocated */
  100. void clear_alloc_stack
  101.   (
  102.     /* pointer to allocation stack */
  103.     ALLOC_STACK *as
  104.   )
  105.   {
  106.     void *p;
  107.  
  108.     while (as->block_list)
  109.       {
  110.     p = as->block_list;
  111.     as->block_list = *((void **) p);
  112.     free(p);
  113.       }
  114.  
  115.     /* set free index so that a new block will be allocated on the
  116.        1st call to get_alloc_stack() */
  117.     as->free = as->n_units_in_block;
  118.  
  119.     return;
  120.   }
  121.